/**
 * @file XYXKC_IOT.h
 * @brief XYXKC's ESPONE Mind+ library.
 * @n this is the iot module of xiaoyingxing
 *
 * @copyright    XYXKC,2024
 * @copyright    MIT Lesser General Public License
 *
 * @author [email](hyc@xyxedu.cn)
 * @date  2024-8-27
 */
#ifndef XYXKC_IOT_H
#define XYXKC_IOT_H
#include <WiFi.h>
#include <PubSubClient.h>
#include <vector>
#include <iostream>
using namespace std;

#define CUSTOMTOPICNUMBER 20
#define XPORTGETCOREID 1
typedef void (*MsgHandleCb) (String& message);
typedef void (*HandleCb) (void);
class XYXKC_IOT {
public:
    XYXKC_IOT();
    ~XYXKC_IOT();
    void WiFiConnection(String ssid, String password);
    void WiFiHotSpot(String ssid, String password);
    void configureMQTT(String mqtt_user, String mqtt_password);
    void setCustomMqttCallback(const MsgHandleCb handles, const String topic);
    void setCustomMqttCallback(const String topic, const String delimiters, const HandleCb handles);
    void setCustomMqttCallback(const MsgHandleCb handles, const int topicNumber);
    void setCustomMqttCallback(const int topicNumber, const String delimiters, const HandleCb handles);
    void connectMQTT();
    void publishMessage(const String topic, const String payload, int qos = 0);
    void publishMessage(const int topicNumber, const String payload, int qos = 0);
    void subscribeTopic(const String topic);
    void mqttDisconnect();
    void wifiDisconnect();
    bool isWiFiConnected();
    bool isMQTTConnected();
    int mqttState();
    String getWiFiIP();
    
    void setReconnectWIFI(bool reconnect);
    void setReconnectMQTT(bool reconnect);

    void reconnectWIFI();
    void reconnectMQTT();
    MsgHandleCb    mqtt_customMsgHandleCb[CUSTOMTOPICNUMBER] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
    vector<HandleCb> mqtt_customHandleCb[CUSTOMTOPICNUMBER];
    vector<String> mqtt_customDelimiters[CUSTOMTOPICNUMBER];
    vector<String> mqtt_customTopicArray;
    uint8_t customTopicnum;

    String ssid;
    String password;
    String mqtt_user;
    String mqtt_password;
    String mqtt_server = "43.139.167.119"; // 固定的MQTT服务器地址
    int mqtt_port = 1883; // 固定的MQTT端口
    bool apMode;
    bool autoReconnectWiFi = true;
    bool autoReconnectMQTT = true;
private:



};

extern WiFiClient espClient;

extern PubSubClient client;
#endif
